// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/accesscontrol/api/AccessControlPointApi'

import ComClass from '@api/ComQuery'

import ListClass from '@api/accesscontrol/model/AccessControlPointDto'
import DelClass from '@api/accesscontrol/model/AccessControlPointDeleteReq'
import BindClass from '@api/accesscontrol/model/AccessControlPointBindReq'
import SubmitClass from '@api/accesscontrol/model/AccessControlPointSyncReq'

const AddClass = ListClass
const EditClass = ListClass

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  async getList(param, TIP = '查询列表') {
    const DTO = CORE.grpcDTO(ListClass, param, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'accessControlPointQuery', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },
  transList(arr) {
    arr.forEach((item) => {
      item.oldAreaId = item.regionId
    })
    return arr
  },

  // —————————————————————————————— 增 —————————————————————————————— //

  async add(one, TIP = '新增') {
    const DTO = CORE.grpcDTO(AddClass, one)
    await CORE.grpc(TIP, CLIENT, 'accessControlPointSave', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 改 —————————————————————————————— //

  async edit(one, TIP = '修改') {
    const DTO = CORE.grpcDTO(EditClass, one)
    await CORE.grpc(TIP, CLIENT, 'accessControlPointUpdate', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: id })
    await CORE.grpc(TIP, CLIENT, 'accessControlPointDelete', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 绑定 —————————————————————————————— //

  async bind(one, TIP = '绑定设备') {
    const DTO = CORE.grpcDTO(BindClass, one)
    await CORE.grpc(TIP, CLIENT, 'accessControlPointBind', DTO)
    return CORE.grpcOK(TIP)
  },

  // —————————————————————————————— 提交 —————————————————————————————— //

  async submit(one, TIP = '提交') {
    const DTO = CORE.grpcDTO(SubmitClass, one)
    await CORE.grpc(TIP, CLIENT, 'accessControlPointSync', DTO)
    return CORE.grpcOK(TIP)
  },
}
